﻿using UnityEngine;
using System.Collections.Generic;

#if UNITY_EDITOR

using UnityEditor;

#endif

namespace ProjectMG
{
    /// <summary>
    /// 配置创建角色时各个门派的站位及摄像机（编辑器运行时可选择预览项）
    /// 孙希强 2018.05.19
    /// </summary>
    ///

    public class CreateRoleConfig : MonoBehaviour
    {
        private class AudioItem
        {
            public AudioSource AudioSource
            {
                get; set;
            }

            public bool InitMute
            {
                get; set;
            }
        }

        [System.Serializable]
        public class Section
        {
            /// <summary>
            /// 名称，必须匹配数据表"UIMenpaiData.txt"中的"SectionName"配置
            /// </summary>
            public string name;

            /// <summary>
            /// 摄像机，选定时开启，否则关闭
            /// </summary>
            public Camera camera;

            /// <summary>
            /// 摄像机目标位置
            /// </summary>
            public Vector3 cameraTargetPosition;

            /// <summary>
            /// 摄像机目标朝向
            /// </summary>
            public Vector3 cameraTargetRotation;

            /// <summary>
            /// 男角位置
            /// </summary>
            public Transform roleMale;

            /// <summary>
            /// 女角位置
            /// </summary>
            public Transform roleFemale;

            /// <summary>
            /// 环境节点，选定时开启，否则关闭
            /// </summary>
            public GameObject[] environment;

            /// <summary>
            /// 天空色
            /// </summary>
            public Color skyColor;

            /// <summary>
            /// 雾色
            /// </summary>
            public Color fogColor;

            /// <summary>
            /// 雾模式
            /// </summary>
            public FogMode fogMode;

            /// <summary>
            /// 雾浓度
            /// </summary>
            public float fogDensity;

            /// <summary>
            /// 角色主光参数
            /// </summary>
            public Vector4 roleMainLightAngle;

            /// <summary>
            /// 角色主光颜色
            /// </summary>
            public Color roleMainLightColor;

            /// <summary>
            /// 角色次光参数
            /// </summary>
            public Vector4 roleSecondLightAngle;

            /// <summary>
            /// 角色次光颜色
            /// </summary>
            public Color roleSecondLightColor;

            /// <summary>
            /// 保留对象，以便于后期临时扩充内容
            /// </summary>
            public GameObject[] reserved;

            /// <summary>
            /// 存储环境音效对象及初始状态
            /// </summary>
            private List<AudioItem> _evnAdioList = new List<AudioItem>();

            public void RefreshSection(bool visible)
            {
                if (camera)
                {
                    camera.gameObject.SetActive(visible);
                }

                for (int e = 0; e < environment.Length; ++e)
                {
                    GameObject envObj = environment[e];
                    if (envObj)
                    {
                        envObj.SetActive(visible);
                    }
                }

                if (visible)
                {
                    RenderSettings.ambientSkyColor = skyColor;
                    RenderSettings.fogColor = fogColor;
                    RenderSettings.fogColor = fogColor;
                    RenderSettings.fogMode = fogMode;
                    RenderSettings.fogDensity = fogDensity;
                }
            }

            public void InitEvnAudioItems()
            {
                _evnAdioList.Clear();
                for (int e = 0; e < environment.Length; ++e)
                {
                    GameObject envObj = environment[e];
                    if (envObj)
                    {
                        AudioSource[] components = envObj.GetComponentsInChildren<AudioSource>();
                        for (int i = 0; i < components.Length; ++i)
                        {
                            AudioSource audio = components[i];
                            _evnAdioList.Add(new AudioItem() { AudioSource = audio, InitMute = audio.mute });
                        }
                    }
                }
            }

            public void SetEvnAudioMute(bool value)
            {
                for (int i = 0; i < _evnAdioList.Count; ++i)
                {
                    AudioItem audioItem = _evnAdioList[i];
                    audioItem.AudioSource.mute = value ? true : audioItem.InitMute;
                }
            }
        }

        public Section[] sections;

        public int Selection
        {
            get
            {
                return _selection;
            }
            set
            {
                _selection = value;
            }
        }

        private int _selection = -1;

        private void Awake()
        {
            for (int i = 0; i < sections.Length; ++i)
            {
                Section section = sections[i];
                section.InitEvnAudioItems();
                section.RefreshSection(false);
            }
        }

        public Section Select(string sectionName)
        {
            int index = FindSection(sectionName);
            if (index != -1)
            {
                return Select(index);
            }
            else
            {
                return null;
            }
        }

        public Section Select(int index)
        {
            if (index < 0 || index >= sections.Length)
            {
                return null;
            }

            if (index == Selection)
            {
                return sections[Selection];
            }

            for (int i = 0; i < sections.Length; ++i)
            {
                if (i != index)
                {
                    sections[i].RefreshSection(false);
                }
            }

            sections[index].RefreshSection(true);
            Selection = index;
            return sections[index];
        }

        private int FindSection(string sectionName)
        {
            for (int i = 0; i < sections.Length; ++i)
            {
                if (sections[i].name == sectionName)
                {
                    return i;
                }
            }

            return -1;
        }
    }

#if UNITY_EDITOR

    [CustomEditor(typeof(CreateRoleConfig))]
    public class CreateRoleConfigEditor : Editor
    {
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();

            CreateRoleConfig config = (CreateRoleConfig)target;
            if (Application.isPlaying)
            {
                string[] text = new string[config.sections.Length];
                for (int i = 0; i < config.sections.Length; ++i)
                {
                    text[i] = config.sections[i].name;
                }

                int index = EditorGUILayout.Popup(config.Selection, text);
                config.Select(index);
            }
        }
    }

#endif
}
